package com.erli.exchange.common.exception;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import com.erli.exchange.controller.BaseController;
import com.erli.exchange.core.beans.api.ApiException;
import com.erli.exchange.core.beans.api.AuthException;
import com.erli.exchange.core.beans.api.ResultEnum;
import com.erli.exchange.core.beans.api.ResultHandler;

/**
 * 
 * @Package com.erli.exchange.common   
 * @ClassName:  GlobalExceptionHandler   
 * @Description:异常处理
 * @Author: LeeHo
 * @Date:   2019年3月10日 上午11:41:24   
 *
 */
@ControllerAdvice(basePackageClasses = {BaseController.class})
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler{

	@ExceptionHandler(value = {Throwable.class})
	@ResponseBody
	public Object baseApiExceptionHandler(HttpServletRequest req, Throwable e) {
		return ResultHandler.failure(ResultEnum.SERVER_EXCEPTION);
	}
	
	@ResponseBody
	@ExceptionHandler(ApiException.class)
	public Object handlerNoFoundException(ApiException e) {
		return ResultHandler.failure(e.getCode(), e.getMsg());
	}
	
	@ResponseBody
	@ExceptionHandler(AuthException.class)
	public Object handAuthException(AuthException e) {
		return ResultHandler.failure(e.getCode(), e.getMsg());
	}
}
